<template>
  <div class="login-container">
    <Card title="系统登录" class="login-card">
      <Form :model="formState" @finish="handleLogin">
        <FormItem name="username" :rules="[{ required: true, message: '请输入用户名' }]">
          <Input v-model:value="formState.username" placeholder="用户名" size="large">
          <template #prefix>
            <UserOutlined />
          </template>
          </Input>
        </FormItem>

        <FormItem name="password" :rules="[{ required: true, message: '请输入密码' }]">
          <InputPassword v-model:value="formState.password" placeholder="密码" size="large">
            <template #prefix>
              <LockOutlined />
            </template>
          </InputPassword>
        </FormItem>

        <FormItem>
          <Button type="primary" html-type="submit" size="large" block :loading="loading">
            登录
          </Button>
        </FormItem>
      </Form>
    </Card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { Form, FormItem, InputPassword, Card, message, Button, Input } from 'ant-design-vue'
import { useAuthStore } from '@/stores/auth'
import { PageEnum } from "@/enums/pageEnum";

const router = useRouter()
const authStore = useAuthStore()

const formState = reactive({
  username: '',
  password: ''
})

const loading = ref(false)

const handleLogin = async () => {
  loading.value = true
  try {
    await authStore.login(formState)
    message.success('登录成功')
    // router.push(PageEnum.BASE_HOME)
    router.push(PageEnum.ACCOUNT_USER)
  } catch (error) {
    message.error(error instanceof Error ? error.message : '登录失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background: #ebeff8;
  display: flex;
  flex-direction: row;
  justify-content: start;
  align-items: center;
  height: 100vh;

  .login-card {
    border-radius: 12px;
    width: 350px;
    margin: auto;

    input {
      background: #fff !important;
    }
  }
}
</style>